<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="$store.state.inputVal" @change="changeVal" />
    <a-button type="primary" @click="addFn">添加事项</a-button>

    <a-list bordered :dataSource="$store.getters.infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <a-checkbox :checked="item.done" @change="changeFn($event, item.id)">{{item.info}}</a-checkbox>
        <a slot="actions" @click="delFn(item.id)">删除</a>
      </a-list-item>

      <div slot="footer" class="footer">
        <span>{{$store.getters.noDone}}条剩余</span>
        <a-button-group>
          <a-button :type="flag === 'all' ? 'primary' : ''" @click="changeHight('all')">全部</a-button>
          <a-button :type="flag === 'nodone' ? 'primary' : ''" @click="changeHight('nodone')">未完成</a-button>
          <a-button :type="flag === 'done' ? 'primary' : ''" @click="changeHight('done')">已完成</a-button>
        </a-button-group>
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: 'app',
  data() {
    return {
      list: [
       
      ]
    }
  },
  methods: {
    changeVal(e) {
      this.$store.commit('setInputVal', e.target.value)
    },
    addFn() {
      if(this.$store.state.inputVal.trim().length === 0) {
        return this.$message.info('请填写内容')
      }

      this.$store.commit('addList')
    },
    delFn(id){
      this.$store.commit('delList', id)
    },
    changeFn(el, id) {
      let obj = {
        done: el.target.checked, 
        id
      }
      this.$store.commit('changeFn', obj)
    },
    clearDone() {
      this.$store.commit('clearDone')
    },
    changeHight(flag) {
      this.$store.commit('changeHight', flag)
    }
  },
  created() {
    this.$store.dispatch('setList')
  },
  computed: {
    ...mapState(['flag'])
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
